package sliver.action.advertisement;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.service.TagManager;

import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("advertisement.calculateCpmAction")
public class CalculateCpmAction extends ActionSupport{

	private static final long serialVersionUID = 4699037812850032302L;
	
	private Integer position;
	
	private List<Integer> tagIds = new ArrayList<Integer>();
	private List<Long> provinceIds = new ArrayList<Long>();
	private String showTimes;
	
	@Autowired
	private TagManager tagManager;
	
	public CalculateCpmAction(){
		
	}
	
	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;
	}

	public List<Integer> getTagIds() {
		return tagIds;
	}

	public void setTagIds(List<Integer> tagIds) {
		this.tagIds = tagIds;
	}

	public List<Long> getProvinceIds() {
		return provinceIds;
	}

	public void setProvinceIds(List<Long> provinceIds) {
		this.provinceIds = provinceIds;
	}
	
	public String getShowTimes() {
		return showTimes;
	}

	public void setShowTimes(String showTimes) {
		this.showTimes = showTimes;
	}

	@Override
	public String execute() throws Exception {
		Double suggestPrice = tagManager.calculateCpm(tagIds, position, provinceIds ,showTimes);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/plain");
		response.getWriter().write(String.valueOf(suggestPrice));
		
		return null;
	}
}
